/* (c)2010-2013 Andrea Caltroni (andrea.caltroni@pd.infn.it), INFN (www.infn.it)
 *
 * This file is part of the IGI Survey Portlet (from here on shortened to IGISP).
 * IGISP is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) any 
 * later version.
 *
 * IGISP is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with IGISP. If not, see <http://www.gnu.org/licenses/>.
 */
package it.infn.grid.liferay.survey.modules.admin.web;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.ajm.spring.survey.modules.admin.model.Survey;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;


/**
 * Validator class for the Survey bean.
 * 
 * @author Andrea Caltroni, andrea.caltroni@pd.infn.it, INFN
 */
@Component("surveyValidator")
public class SurveyValidator implements Validator {

    static Logger logger = Logger.getLogger(SurveyValidator.class);
    
    // ------------------------------------------------------------------------|
    
    /**
     * This Validator validates *just* Survey instances.
     * 
     * @see org.springframework.validation.Validator#supports(java.lang.Class)
     */
    public boolean supports(Class<?> clazz) {
        
        return Survey.class.equals(clazz);
    }

    // ------------------------------------------------------------------------|
    
    /**
     * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
     */
    public void validate(Object obj, Errors errors) {
        
        Survey survey = (Survey)obj;
        
        Date expDate = survey.getExpiration();
        Date startDate = survey.getStart();

        // validates name
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "survey.name.required");
        
        // reject if expiration is empty
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "expiration", "survey.expiration.required");
        
        Calendar calendar = GregorianCalendar.getInstance();
        // reject if start or expiration are invalid dates
        try {
            calendar.setTime(startDate);
        }
        catch (Exception exc) {
            errors.rejectValue("expiration", "survey.start.invalid");
        }

        try {
            calendar.setTime(expDate);
        }
        catch (Exception exc) {
            errors.rejectValue("expiration", "survey.expiration.invalid");
        }

        // reject if expiration is in the past
        if (expDate != null) {
            if (!errors.hasFieldErrors("expiration")) {
                
                if (expDate.before(calendar.getTime())) {
                    errors.rejectValue("expiration", "survey.expiration.invalid");
                }
            }
        }
        
        // validates order of start and expiration dates
        if (startDate != null && expDate != null) {
            if (startDate.after(expDate)) {
                errors.rejectValue("expiration", "survey.dates.invalid");
            }
        }
    }

    // ------------------------------------------------------------------------|
    
}
